package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2023-02-18
 * Time: 17:26
 */
@RestController("/web")    //是@ResponseBody和Controller的组合注解
public class WebController {

    //路由,访问的url(不能省略)，method属性是用来限制请求访问的方法
    //@RequestMapping(value = "/hi",method = RequestMethod.GET)
    //@PostMapping("/hi") //作用和上面一样，是上面这种写法简化而设计出来的注解
    @GetMapping("/hi") //作用和上面一样，是上面这种写法简化而设计出来的注解
    public String sayHi(HttpServletRequest request, HttpServletResponse response) {

        return "Hi," + request.getParameter("name");
    }

    //获取单个参数
    @GetMapping("/get1")
    public String getParam1(Integer age) {
        return "value: " + age;
    }

    //获取多个参数
    @GetMapping("/get2")
    public String getParam2(String name,Integer age) {
        return "value1: "+ name + "| value2: "+age;
    }

    //获取对象
    @GetMapping("/get3")
    public String getParam3(Student student) {
        return student.toString();
    }

    //参数获取form表单
    @RequestMapping("/login")
    public String login(String name, String password) {
        return "<h1>用户名: " +name+ "密码:"+ password+ "</h2>";
    }
    //对象获取form表单
    @RequestMapping("/reg")
    public String reg(User user) {
        return user.toString();
    }

    //获取JSON对象
    @RequestMapping("/login2")
    public HashMap<String,Object> login2(@RequestBody User user) {
        HashMap<String,Object> result = new HashMap<>();
        result.put("name",user.getName());
        result.put("password",user.getPassword());
        return result;
    }

    //获取文件
    @RequestMapping("/reg2")
    public String reg2(String name, @RequestPart("myfile")MultipartFile file) throws IOException {
        file.transferTo(new File("C:\\JavaEE进阶\\spring-mvc-demo\\123.png"));
        return "success";
    }

    //获取cookie值
    @RequestMapping("/getck")
    public String reg3(@CookieValue("zhangsan") String name) {
        return "zhangsan: " + name;
    }

    //设置Session
    @RequestMapping("/setsess")
    public String setSess(HttpServletRequest request) {
        HttpSession session = request.getSession(true);

        if(session != null) {
            session.setAttribute("username","66666");
        }

        return "Session存储成功!";
    }

    //获取会话
    @RequestMapping("/getsess")
    public String getSessi(@SessionAttribute(value = "username",required = false)String username) {
        return "username: " + username;
    }

    //获取请求头(header)里面的信息
    @RequestMapping("/gethead")
    public String getHeader(@RequestHeader("user-Agent")  String userAgent) {
        return "User-Agent:" + userAgent;
    }


    @RequestMapping("/gettime")
    public String getTime(@RequestParam(value = "t")String time) {
        return "time = " + time;
    }

    @RequestMapping("/login3/{name}/{password}")
    public String login3(@PathVariable("name") String username, @PathVariable String password) {
        return username + "|" + password;
    }

}

